package airacrobats.game;

import airacrobats.io.data.StageData;
import airacrobats.util.Util;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 * This class contains the stage state.
 */
public class Stage
{
    /**
     * The info panel size.
     */
    private static final Dimension INFO_PANEL_SIZE = new Dimension(120, 40);
    
    /**
     * Stage data.
     */
    private StageData stageData;
    /**
     * Image.
     */
    private BufferedImage image;
    /**
     * Score.
     */
    private int score;
    /**
     * Record.
     */
    private int scoreRecord;
    /**
     * The remaining time of the stage.
     */
    private long remainingTime;
    /**
     * Flag indicating if the score is visible.
     */
    private boolean scoreVisible;

    /**
     * @param stageData The stage data.
     * @param image The image.
     * @param scoreRecord The record.
     */
    public Stage(StageData stageData, BufferedImage image, int scoreRecord)
    {
        this.stageData = stageData;
        this.image = image;
        this.scoreRecord = scoreRecord;
    }

    /**
     * Sets all the stage attributes.
     * @param scoreVisible Score visible flag.
     */
    public void start(boolean scoreVisible)
    {
        this.scoreVisible = scoreVisible;
        score = 0;
        remainingTime = stageData.getDuration() * 1000;
    }
    
    /**
     * Creates the info panel and adds it to the owner panel.
     * @param owner The owner panel.
     * @param c The grid bag constraints.
     */
    public void createInfoPanel(JPanel owner, GridBagConstraints c)
    {
        JPanel panel = new JPanel()
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                int height = g.getFontMetrics().getHeight();
                Util.drawOutlinedString("Time: " + (remainingTime / 1000) + "s", Color.WHITE, Color.BLACK, g, 2, height);

                if (!scoreVisible)
                    return;

                if (score > scoreRecord)
                    Util.drawOutlinedString("Score: " + score, Color.GREEN.darker(), Color.BLACK, g, 2, height * 2);
                else
                    Util.drawOutlinedString("Score: " + score, Color.WHITE, Color.BLACK, g, 2, height * 2);
            }
        };
        panel.setPreferredSize(INFO_PANEL_SIZE);
        panel.setMinimumSize(INFO_PANEL_SIZE);
        owner.add(panel, c);
    }

    /**
     * @return The stageData.
     */
    public StageData getStageData()
    {
        return stageData;
    }

    /**
     * @return The image.
     */
    public BufferedImage getImage()
    {
        return image;
    }

    /**
     * @return The score.
     */
    public int getScore()
    {
        return score;
    }

    /**
     * @param score The new score.
     */
    public void setScore(int score)
    {
        this.score = score;
    }

    /**
     * @return The scoreRecord.
     */
    public int getScoreRecord()
    {
        return scoreRecord;
    }

    /**
     * @param scoreRecord The new score record.
     */
    public void setScoreRecord(int scoreRecord)
    {
        this.scoreRecord = scoreRecord;
    }

    /**
     * @return The remaining time.
     */
    public long getRemainingTime()
    {
        return remainingTime;
    }

    /**
     * @param remainingTime The remaining time to set.
     */
    public void setRemainingTime(long remainingTime)
    {
        this.remainingTime = remainingTime;
    }
}
